package com.gxa.order.controller;

import com.gxa.common.base.RespResult;
import com.gxa.order.dto.CommentDto;
import com.gxa.order.service.AccommodationOrderAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/accommodationorder")
public class AccommodationOrderAppController {
    @Autowired
    private AccommodationOrderAppService accommodationOrderService;

    /**
     * 修改订单状态
     */
    @RequestMapping("/update")
    public RespResult update(Long order_num,String status){
        RespResult update = accommodationOrderService.updateOrderStatusByOrderNo(order_num,status);
        return update;
    }
    /**
     * 添加评论
     */
    @RequestMapping("/addcomment")
    public RespResult addComment(@RequestBody CommentDto commentDto){
        commentDto.setComments_datetime(new Date());
        RespResult add = accommodationOrderService.addComment(commentDto);
        if(commentDto.getPath()!=null) {
            int id = commentDto.getAccommodation_comments_id();
            int n = accommodationOrderService.addImg(commentDto.getPath(), id);
            if (n <= 0) {
                System.out.println("上传失败");
            }
        }
        return add;
    }

//获取图片保存路径
    @RequestMapping("/upload")
    public String upload(List<MultipartFile> file,  HttpServletRequest request) throws IOException {
        String Path = null;
        for (int i = 0; i < file.size(); i++) {
            String uploadPath=null;
            try {
                 uploadPath = new File("").getCanonicalPath() + File.separator + "ocean-order/src/main/upload";
            } catch (IOException e) {
                throw new RuntimeException("获取项目路径失败", e);
            }
            System.out.println(uploadPath);
            HttpSession session = request.getSession();
            String filename = file.get(0).getOriginalFilename();
            System.out.println(filename);

            // 确定文件存放的路径
            File file2 = new File(uploadPath);
            if (!file2.exists()){
                file2.mkdir();
            }
            String fname= UUID.randomUUID()+"_"+filename;
            session.setAttribute("en",fname);
            String s = uploadPath + "/" + fname;//完整路径
            file.get(i).transferTo(new File(s));
            FileInputStream inputStream = new FileInputStream(s);
            System.out.println(request.getContextPath());
            FileOutputStream outputStream = new FileOutputStream(file2+"/"+fname);
            byte[] bytes = new byte[1024];
            int b=-1;
            while ((b=inputStream.read(bytes))!=-1){
                outputStream.write(bytes);
            }
            Path=Path+"/"+uploadPath;
        }
      return Path;
    }

    /**
     * 查询支付订单信息
     */
    @RequestMapping("/selectpayorder")
    public RespResult selectpayorder(Long order_num){
        RespResult r = accommodationOrderService.selectpayorder(order_num);
        return r;
    }
}
